package pl.edu.pw.polygen.generator;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Component;

@Component("triangularGeneratorService")
public class TriangularGeneratorServiceImpl implements TriangularGeneratorService{

	@Override
	public void executeTriangle(List<String> args) {
		 try {
			  List<String> command = new ArrayList<String>();
			  command.add("c:\\polygen\\triangle\\triangle.exe");
			  if(args!=null){
				  for(String arg : args){
					  command.add(arg);
				  }
			  }
		      ProcessBuilder builder = new ProcessBuilder(command);
		    // Map<String, String> environ = builder.environment();
		     // builder.directory(new File("D:\\inzynierka\\triangle"));

		      //System.out.println("Directory : " + "D:\\inzynierka\\triangle" );
		      final Process process = builder.start();
		      InputStream is = process.getInputStream();
		      InputStreamReader isr = new InputStreamReader(is);
		      BufferedReader br = new BufferedReader(isr);
		      String line;
		      while ((line = br.readLine()) != null) {
		        System.out.println(line);
		      }
		      System.out.println("Program terminated!");
		    }
		    catch (Exception err) {
		      err.printStackTrace();
		    }
	}

}
